#include "gui.hh"

Gui::Gui(): m_box0{Gtk::Orientation::ORIENTATION_VERTICAL}, m_box1{Gtk::Orientation::ORIENTATION_VERTICAL}{
  set_hierarchy();
  draw_widgets();
};

void Gui::on_button_calculate(){
  
  std::string cm = m_entry0.get_text();
  std::string kg = m_entry1.get_text();
  
  Bmi bmi(std::stof(cm), std::stof(kg));

  float BMI = bmi.getBmi();

  std::string type;
  if (BMI < 17){
    type = "Muito abaixo do peso";
  } else if (BMI >= 17 && BMI < 18.5){
    type = "Abaixo do peso";
  } else if (BMI >= 18.5 && BMI < 25){
    type = "Normal";
  } else if (BMI >= 25 && BMI < 30){
    type = "Acima do peso";
  } else if (BMI >= 30 && BMI < 35){
    type = "Obesidade I";
  } else if (BMI >= 35 && BMI < 40){
    type = "Obesidade II (severa)";
  } else if (BMI >= 40){
    type = "Obesidade III (mórbida)";
  }

  std::ostringstream ss;
  ss << BMI;
  std::string s(ss.str());

  m_label2.set_text("Seu IMC é "+s+"\n"+type);
  
}

void Gui::set_hierarchy(){
  add(m_fixed0);

  m_fixed0.add(m_box0);
  m_fixed0.add(m_box1);
  m_fixed0.add(m_box2);
  m_fixed0.add(m_box3);
  m_fixed0.move(m_box1, 2, 60);
  m_fixed0.move(m_box2, 4, 120);
  m_fixed0.move(m_box3, 6, 180);
  
  m_alignment0.add(m_box0);
  m_alignment1.add(m_box1);
  m_alignment2.add(m_box2);
  m_alignment3.add(m_box3);
  
  m_box0.add(m_label0);
  m_box0.add(m_entry0);

  m_box1.add(m_label1);
  m_box1.add(m_entry1);

  m_box2.add(m_button0);

  m_box3.add(m_label2);

};

void Gui::draw_widgets(){
  set_title("Calculadora de IMC");
  set_default_size(10, 20);
  set_visible(true);
  set_can_focus(false);
  set_resizable(true);
  set_border_width(6);

  m_fixed0.set_size_request(100, 125);
  m_fixed0.set_visible(true);
  m_fixed0.set_can_focus(false); 

  m_alignment0.set_size_request(100, 20);
  m_alignment0.set_visible(true);
  m_alignment0.set_can_focus(true);
  m_alignment0.set_border_width(10);

  m_alignment1.set_size_request(100, 20);
  m_alignment1.set_visible(true);
  m_alignment1.set_can_focus(true);
  m_alignment1.set_border_width(10);

  m_box0.set_visible(true);
  m_box1.set_visible(true);
  m_box2.set_visible(true);
  m_box3.set_visible(true);

  m_label0.set_text("Altura em cm");
  m_label0.set_visible( true );
  
  m_label1.set_text("Massa em kg");
  m_label1.set_visible( true );

  m_label2.set_visible(true);
  
  m_entry0.set_visible( true );
  m_entry0.set_placeholder_text("Informe a sua altura");
  m_entry0.set_input_purpose( Gtk::INPUT_PURPOSE_NUMBER);
  m_entry0.set_max_length(3);

  m_entry1.set_visible( true );
  m_entry1.set_placeholder_text("Informe a sua massa");
  m_entry1.set_input_purpose(Gtk::INPUT_PURPOSE_NUMBER);
  m_entry1.set_max_length(3);
  
  m_button0.set_label("Calcular");
  m_button0.set_visible(true);
  m_button0.set_can_focus(true);
  m_button0.set_focus_on_click(true);
  m_button0.set_border_width(5);
  m_button0.signal_clicked().connect( sigc::mem_fun( *this, &Gui::on_button_calculate ) );
};
